package spring.security.wiremock;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.removeAllMappings;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;

import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.core.io.ClassPathResource;

public class MockServer {
	
	public static void main(String[] args) throws IOException {
		//运行的端口
		configureFor(8066);
		//情况配置
		removeAllMappings();
		
		mock("mock/response/01.json", "/user/1");
		mock("mock/response/02.json", "/user/2");
		
	}

	private static void mock(String filePath, String url) throws IOException {
		ClassPathResource res = new ClassPathResource(filePath);
		List<String> readLines = FileUtils.readLines(res.getFile(),"UTF-8");
		String content = StringUtils.join(readLines, '\n');
		
		stubFor(get(urlPathEqualTo(url))
				.willReturn(
						aResponse()
						.withBody(content)
						.withStatus(200)
						)
				);
	}
	
}
